<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="../vue.js"></script>
  </head>

  <body>
    <h1>人员列表</h1>
    <div id="app">
      <input type="text" placeholder="请输入要搜索的名称" v-model="keyword" />
      <ul>
        <li v-for="item in showinfo" :key="item.id">
          {{item.name}}---{{item.age}}---{{item.sex}}
        </li>
      </ul>
    </div>
    <script>
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            keyword: "",
            persons: [
              {
                id: 001,
                name: "马冬梅",
                age: 18,
                sex: "女",
              },
              {
                id: 002,
                name: "周冬雨",
                age: 19,
                sex: "女",
              },
              {
                id: 003,
                name: "周杰伦",
                age: 20,
                sex: "男",
              },
              {
                id: 004,
                name: "温兆伦",
                age: 21,
                sex: "男",
              },
            ],
            filPersons: [], // 需要渲染到页面的数据
          };
        },
        // 用 computed 实现
        computed: {
          showinfo() {
            return this.persons.filter((item) => {
              return item.name.indexOf(this.keyword) !== -1;
            });
          },
        },

        // 通过 watch  来 实现列表筛选
        //     watch: {
        //       keyword: {
        //         immediate: true, //  首次人员列表获取空字符串数据
        //         handler(val) {
        //           this.filPersons = this.persons.filter((item) => {
        //             // indexOf 查看 " " 字符串时索引值不为-1  而是为1
        //             return item.name.indexOf(val) !== -1;
        //           });
        //         },
        //       },
        //     },
      });
    </script>
  </body>
</html>
